package alva.com.getcamera.utils;

import android.graphics.Point;
import android.hardware.Camera;
import android.util.DisplayMetrics;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import alva.com.getcamera.app.MyApplication;

import static alva.com.getcamera.R.id.pictureSize;

/**
 * Created by Administrator on 2016/11/23 0023.
 */

public class CameraUtils {

    /**
     * find the ratio is 16:9 size of picture
     *
     * @param camera
     * @return
     */
    public static Camera.Size findBestPictureSize(Camera camera) {
        int diff = Integer.MIN_VALUE;
        Camera.Parameters parameters = camera.getParameters();
        String pictureSizeValueString = parameters.get("picture-size-values");
        if (pictureSizeValueString == null) {
            pictureSizeValueString = parameters.get("picture-size-value");
        }
        if (pictureSizeValueString == null) {
            return camera.new Size(getScreenWH().widthPixels, getScreenWH().heightPixels);
        }
        int bestX = 0;
        int bestY = 0;
        String[] pictureSizes = pictureSizeValueString.split(",");
        for (String pictureSize : pictureSizes) {
            pictureSize = pictureSize.trim();
            int dimPosition = pictureSize.indexOf("x");
            if (dimPosition == -1) {
                Log.e("pictureSizeException", "Bad pictureSizeString:" + pictureSize);
                continue;
            }
            int newX = 0;
            int newY = 0;
            newX = Integer.parseInt(pictureSize.substring(0, dimPosition));
            newY = Integer.parseInt(pictureSize.substring(dimPosition + 1));
            Log.i("hhhhhh", "findBestPictureSize: " + newX + "*" + newY);
            Point screenResolution = new Point(getScreenWH().widthPixels, getScreenWH().heightPixels);
            int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);
            if (newDiff == diff) {
                bestX = newX;
                bestY = newY;
            } else if (newDiff > diff) {
                if (newX * 9 == newY * 16) {
                    bestX = newX;
                    bestY = newY;
                    diff = newDiff;
                }
            }
        }
        if (bestX > 0 && bestY > 0) {
            return camera.new Size(bestX, bestY);
        }
        return null;
    }

    /**
     * find the ratio is 16:9 size of preview
     *
     * @param camera
     * @return
     */
    public static Camera.Size findBestPreviewSize(Camera camera) {
        int diff = Integer.MAX_VALUE;
        Camera.Parameters parameters = camera.getParameters();
        String previewSizeString = parameters.get("preview-size-values");
        if (previewSizeString == null) {
            previewSizeString = parameters.get("preview-size-value");
        }
        if (previewSizeString == null) {
            return camera.new Size(getScreenWH().widthPixels, getScreenWH().heightPixels);
        }
        int bestX = 0;
        int bestY = 0;
        String[] previewSizes = previewSizeString.split(",");
        for (String previewSize : previewSizes) {
            previewSize = previewSize.trim();
            int dimPosition = previewSize.indexOf("x");
            if (dimPosition == -1) {
                Log.e("previewSizeException", "Bad pictureSizeString:" + pictureSize);
                continue;
            }
            int newX = 0;
            int newY = 0;
            newX = Integer.parseInt(previewSize.substring(0, dimPosition));
            newY = Integer.parseInt(previewSize.substring(dimPosition + 1));
            Log.i("hhh", "findBestPictureSize: " + newX + "*" + newY);
            Point screenResolution = new Point(getScreenWH().widthPixels, getScreenWH().heightPixels);
            int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);
            if (newDiff == diff) {
                bestX = newX;
                bestY = newY;
            } else if (newDiff < diff) {
                if (newX * 9 == newY * 16) {
                    bestX = newX;
                    bestY = newY;
                    diff = newDiff;
                }
            }
        }
        if (bestX > 0 && bestY > 0) {
            return camera.new Size(bestX, bestY);
        }
        return null;
    }

    public static String[] get16And9PictureSizes(Camera camera) {
        String[] pictureSizes = null;
        float ratio = (float) (16.0 / 9.0);
        Camera.Parameters parameters = camera.getParameters();
        String pictureSizeString = parameters.get("picture-size-values");
        if (pictureSizeString == null) {
            pictureSizeString = parameters.get("picture-size-value");
        }
        if (pictureSizeString == null) {
            pictureSizes[0] = getScreenWH().heightPixels + "x" + getScreenWH().widthPixels;
            return pictureSizes;
        }
        String[] pictures = pictureSizeString.split(",");
        List<String> sizeList = new ArrayList<>();
        for (int i = 0; i < pictures.length; i++) {
            int dimPosition = pictures[i].indexOf("x");
            int width = Integer.parseInt(pictures[i].substring(0, dimPosition));
            int height = Integer.parseInt(pictures[i].substring(dimPosition + 1));
            float tempRatio = Float.parseFloat(width + "") / Float.parseFloat(height + "");
            if (tempRatio == ratio) {
                sizeList.add(width + "x" + height);
            }
        }
        pictureSizes = new String[sizeList.size()];
        for (int i = 0; i < sizeList.size(); i++) {
            pictureSizes[i] = sizeList.get(i);
        }
        return pictureSizes;
    }

    public static DisplayMetrics getScreenWH() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        displayMetrics = MyApplication.context.getResources().getDisplayMetrics();
        return displayMetrics;
    }
}
